home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / git-4.3 / git-4 / git-4.3.11 / src / xid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  3.0 KB  |  133 lines

  1. /* xid.c -- Cached versions of the getpwuid/getgrgid functions.  These
  2.    versions return only the user/group name, not the entire structure.  */
  3.  
  4. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Written by Tudor Hulubei and Andrei Pitis.  */
  21.  
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include <stdio.h>
  28.  
  29. #include "xstring.h"
  30. #include "xmalloc.h"
  31. #include "xid.h"
  32.  
  33.  
  34. #define UID_BUCKETS 127
  35. #define GID_BUCKETS 127
  36.  
  37.  
  38. typedef struct tag_xuid_t
  39. {
  40.     uid_t uid;
  41.     char name[8];
  42.     struct tag_xuid_t *next;
  43. } xuid_t;
  44.  
  45.  
  46. typedef struct tag_xgid_t
  47. {
  48.     gid_t gid;
  49.     char name[8];
  50.     struct tag_xgid_t *next;
  51. } xgid_t;
  52.  
  53.  
  54. xuid_t *uid_buckets[UID_BUCKETS];
  55. xgid_t *gid_buckets[GID_BUCKETS];
  56.  
  57.  
  58. /* Search uid in the hash table. If it is there, return a pointer to the
  59.    coresponding string. If it is not, find the /etc/passwd entry and insert
  60.    it in the hash table.  */
  61.  
  62. char *
  63. xgetpwuid(uid)
  64.     uid_t uid;
  65. {
  66.     struct passwd *pwd;
  67.     int index = uid % UID_BUCKETS;
  68.     xuid_t *current = uid_buckets[index];
  69.  
  70.     for (; current; current = current->next)
  71.     if (current->uid == uid)
  72.         return current->name;
  73.  
  74.     /* Load the /etc/passwd entry.  */
  75.     pwd = getpwuid(uid);
  76.  
  77.     current = (xuid_t *)xmalloc(sizeof(xuid_t));
  78.  
  79.     if (pwd)
  80.     sprintf(current->name, "%-7s", pwd->pw_name);
  81.     else
  82.     sprintf(current->name, "%-7d", (int)uid);
  83.  
  84.     /* Add it to the hash table.  */
  85.     current->uid  = uid;
  86.     current->next = uid_buckets[index];
  87.     uid_buckets[index] = current;
  88.  
  89.     /* Return the result.  */
  90.     return current->name;
  91. }
  92.  
  93.  
  94. char *
  95. xgetgrgid(gid)
  96.     gid_t gid;
  97. {
  98.     struct group *grp;
  99.     int index = gid % GID_BUCKETS;
  100.     xgid_t *current = gid_buckets[index];
  101.  
  102.     for (; current; current = current->next)
  103.     if (current->gid == gid)
  104.         return current->name;
  105.  
  106.     /* Load the /etc/group entry.  */
  107.     grp = getgrgid(gid);
  108.  
  109.     current = (xgid_t *)xmalloc(sizeof(xgid_t));
  110.  
  111.     if (grp)
  112.     sprintf(current->name, "%-7s", grp->gr_name);
  113.     else
  114.     sprintf(current->name, "%-7d", (int)gid);
  115.  
  116.     /* Add it to the hash table.  */
  117.     current->gid  = gid;
  118.     current->next = gid_buckets[index];
  119.     gid_buckets[index] = current;
  120.  
  121.     /* Return the result.  */
  122.     return current->name;
  123. }
  124.  
  125.  
  126. /* Initialize the hash tables. */
  127. void
  128. xid_init()
  129. {
  130.     memset((void *)uid_buckets, 0, sizeof(uid_buckets));
  131.     memset((void *)gid_buckets, 0, sizeof(gid_buckets));
  132. }
  133.